package com.example.myfirstprj;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap.CompressFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.tatuas.android.cameraview.AfterShutterListener;
import com.tatuas.android.cameraview.BeforeShutterListener;
import com.tatuas.android.cameraview.CameraFailedListener;
import com.tatuas.android.cameraview.CameraType;
import com.tatuas.android.cameraview.CameraView;
import com.tatuas.android.cameraview.Options;
import com.tatuas.android.cameraview.PictureSize;
import com.tatuas.android.cameraview.PictureType;
import com.tatuas.android.cameraview.Shutter;
import com.tatuas.android.cameraview.ShutterFailedListener;
import com.tatuas.android.cameraview.Thumbnail;
import com.tatuas.android.cameraview.Util;
@SuppressLint("SimpleDateFormat")
public class CaptureScreen extends Screen implements OnClickListener{
    public CaptureScreen() {
        super();
        setLayout(R.layout.capture_scr);
    }

    private ImageView imgTut;
    private CameraView cameraView;
	@Override
	protected void onContentViewCreated(Bundle savedInstanceStates) {
		imgTut = (ImageView) findViewById(R.id.imgTut);
		imgTut.setOnClickListener(this);
		cameraView = (CameraView) findViewById(R.id.cameraView1);
		
        final String savePath = createSavePath();
        final String thumbSavePath = savePath + ".thumb.jpg";
        final Options options = new Options();
		
        cameraView.setCameraType(CameraType.FRONT);
        cameraView.setCameraPreviewFocus(Camera.Parameters.FOCUS_MODE_AUTO);
        cameraView.setCameraFailedListener(new CameraFailedListener() {
            @Override
            public void onFailed(String errorMsg) {
                Toast.makeText(cameraView.getContext(), errorMsg, Toast.LENGTH_LONG)
                        .show();
            }
        });

     // size or calculateScale option uses last set param.
        options.setCalculateScale(4);
        options.setPictureSize(new PictureSize(1600, 1200));
        options.setPictureType(PictureType.PNG);
        options.setQuality(100);
        options.setExecAutoFocusWhenShutter(false);
        options.setRestartPreviewAfterShutter(false);

        final Shutter shutter = new Shutter(cameraView, this.getMainActivity(), options);
        shutter.setBeforeShutterListener(new BeforeShutterListener() {
            @Override
            public void beforeShutter() {
                Toast.makeText(cameraView.getContext(), "Before shutter",
                        Toast.LENGTH_LONG).show();
            }
        });

        shutter.setAfterShutterListener(new AfterShutterListener() {
            @Override
            public void afterShutter() {
                Toast.makeText(cameraView.getContext(), "After shutter",
                        Toast.LENGTH_LONG).show();
            }
        });

        shutter.setShutterFailedListener(new ShutterFailedListener() {
            @Override
            public void onFailed(String errorMessage) {
                Toast.makeText(cameraView.getContext(), "Failed shutter",
                        Toast.LENGTH_LONG).show();
            }
        });

        final Button btnShot = (Button) findViewById(R.id.btnShot);
        btnShot.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
//                 shutter.exec(savePath);
                Thumbnail thumb = new Thumbnail(thumbSavePath, 12, Util
                        .getDisplayRotationValue((Activity) v.getContext()), CameraType.FRONT);
                thumb.setFormat(CompressFormat.JPEG);
                thumb.setQuality(50);
                shutter.exec(savePath, thumb);

                v.setClickable(false);
            }
        });

//        Button restartBtn = (Button) findViewById(R.id.cameraLayoutRestartBtn);
//        restartBtn.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                cameraLayout.removePreview();
//                shutterBtn.setClickable(true);
//            }
//        });
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.imgTut) {
			imgTut.setVisibility(View.GONE);
		}
	}

    public String createSavePath() {
        String savePath;
        String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss")
                .format(new Date());
        String storage = Environment.getExternalStorageDirectory()
                .getAbsolutePath();
        String ext = ".jpg";
        String separator = File.separator;
        savePath = storage + separator + timeStamp + ext;
        return savePath;
    }
}
